// pages/my/profile/profile.js
const { getMe, updateMe } = require("../../../utils/apis");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    nick_name: "",
    avatar: "",
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const token = wx.getStorageSync("token");
    if (token) {
      getMe().then((res) => {
        this.setData({
          avatar: res.data.data.avatar,
          id: res.data.data.id,
          nick_name: res.data.data.nick_name,
        });
      });
    }
  },

  formCommit(e) {
    updateMe({
      nickName: this.data.nick_name,
      icon: this.data.avatar,
    })
      .then((res) => {
        wx.navigateBack();
      })
      .catch((e) => {
        wx.showToast({
          icon: "error",
          title: "更新信息失败",
        });
      });
  },
});
